//
//  TheOrderTableViewCell.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/11.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "TheOrderTableViewCell.h"

@interface TheOrderTableViewCell ()

Strong UIButton *chooseButton;
Strong UILabel *timeLabel;
Strong UILabel *titleLabel;
Strong UILabel *typeLabel;
Strong UILabel *moneyLabel;
Strong UIImageView *titleImageView;

@end

@implementation TheOrderTableViewCell

- (id)initWithCellIdentifier:(NSString *)cellID{
    self = [super initWithCellIdentifier:cellID];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _titleImageView = [UIImageView new];
//        _titleImageView.contentMode = UIViewContentModeScaleAspectFit;
        _titleImageView.backgroundColor = getColor(@"666666");
        
        _timeLabel  = [[UILabel alloc]init];
        _timeLabel.font = DEF_FontSize_14;
        _timeLabel.textColor = getColor(mainColor);
        
        _chooseButton = [[UIButton alloc]init];
        _chooseButton.userInteractionEnabled = YES;
        [_chooseButton setImage:[UIImage imageNamed:@"choose_nor"] forState:UIControlStateNormal];
        [_chooseButton setImage:[UIImage imageNamed:@"choose_sel"] forState:UIControlStateSelected];
        [_chooseButton addTarget:self action:@selector(chooseButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _titleLabel  = [[UILabel alloc]init];
        _titleLabel.font = DEF_FontSize_14;
        _titleLabel.textColor = getColor(textColor);
        _titleLabel.numberOfLines = 0;
        
        _typeLabel  = [[UILabel alloc]init];
        _typeLabel.font = DEF_FontSize_17;
        _typeLabel.textColor = getColor(mainColor);
        _typeLabel.textAlignment = NSTextAlignmentRight;
        
        _moneyLabel  = [[UILabel alloc]init];
        _moneyLabel.font = DEF_FontSize_14;
        _moneyLabel.textColor = getColor(mainColor);
        _moneyLabel.textAlignment = NSTextAlignmentRight;
        
        [self.contentView sd_addSubviews:@[_titleImageView , _titleLabel , _timeLabel , _typeLabel , _chooseButton , _moneyLabel]];
        [self layoutFrame];
    }
    return self;
}

- (void)layoutFrame{
    __weak __typeof(self)weakSelf = self;
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(250 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(24 / WIDTH_5S_SCALE);
    }];
    
    [self.chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo((80 / WIDTH_5S_SCALE  - 30 / WIDTH_5S_SCALE ) / 2 + 12 / WIDTH_5S_SCALE);
        make.width.mas_equalTo(30 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(30 / WIDTH_5S_SCALE);
    }];
    
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.chooseButton.mas_right).offset(5 / WIDTH_5S_SCALE);
        make.top.equalTo(weakSelf.timeLabel.mas_bottom).offset(0);
        make.width.mas_equalTo(48 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(48 / WIDTH_5S_SCALE);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleImageView.mas_right).offset(5 / WIDTH_5S_SCALE);
        make.top.equalTo(weakSelf.timeLabel.mas_bottom).offset(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(- 10 / WIDTH_5S_SCALE);
        make.top.mas_equalTo(15 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(17 / WIDTH_5S_SCALE);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(- 10 / WIDTH_5S_SCALE);
        make.bottom.mas_equalTo(- 15 / WIDTH_5S_SCALE);
        make.height.mas_equalTo(14 / WIDTH_5S_SCALE);
    }];
}

//洗衣订单
- (void)setModelWash:(NSDictionary *)modelWash{
    _modelWash = modelWash;
    if ([self.chooseArray containsObject:[NSString stringWithFormat:@"%d",(int)self.indexPath.row]]) {
        self.chooseButton.selected = YES;
    }else{
        self.chooseButton.selected = NO;
    }
    self.timeLabel.text = [NSString stringWithFormat:@"下单时间:%@",[modelWash[@"order_time"] substringToIndex:19]];
    self.moneyLabel.attributedText = [self getAttributeName:[NSString stringWithFormat:@"%@",modelWash[@"lo_total"]]];
    
    NSArray *array = modelWash[@"goods"];
    if (array.count > 0) {
        [self.titleImageView sd_setImageWithURL:[NSURL URLOffsetIPWithString:modelWash[@"goods"][0][@"pic"]]];
        self.titleLabel.attributedText = [self getAttributeName:modelWash[@"goods"][0][@"goods_name"] money:[NSString stringWithFormat:@"%@",modelWash[@"goods"][0][@"price"]] number:[NSString stringWithFormat:@"%@",modelWash[@"goods"][0][@"log_num"]]];
    }
    
    //    订单状态(0-未付款/1-代发货(已付款)/2-已到柜(待收货)/3-已收货)
    switch ([modelWash[@"lo_state"] intValue]) {
        case 0:
        {
            self.typeLabel.text = @"待支付";
        }
            break;
        case 1:
        {
            self.typeLabel.text = @"待发货";
        }
            break;
        case 2:
        {
            self.typeLabel.text = @"已到柜";
        }
            break;
        case 3:
        {
            self.typeLabel.text = @"已收货";
        }
            break;
    }
}

//购物订单
- (void)setModel:(NSDictionary *)model{
    _model = model;
    if ([self.chooseArray containsObject:[NSString stringWithFormat:@"%d",(int)self.indexPath.row]]) {
        self.chooseButton.selected = YES;
    }else{
        self.chooseButton.selected = NO;
    }
    [self.titleImageView sd_setImageWithURL:[NSURL URLOffsetIPWithString:model[@"first_picture"]]];
    self.timeLabel.text = [NSString stringWithFormat:@"下单时间:%@",[self.modelSid[@"order_time"] substringToIndex:19]];
    self.titleLabel.attributedText = [self getAttributeName:model[@"c_name"] money:[NSString stringWithFormat:@"%@",model[@"unit_price"]] number:[NSString stringWithFormat:@"%@",model[@"quantity_purchased"]]];
    self.moneyLabel.attributedText = [self getAttributeName:[NSString stringWithFormat:@"%@",self.modelSid[@"so_total"]]];
//    (0-未付款/1-已付款(即代发货)/2-已发货/3-未评价/4-已评价) 
    switch ([self.modelSid[@"so_state"] intValue]) {
        case 0:
        {
            self.typeLabel.text = @"待支付";
        }
            break;
        case 1:
        {
            self.typeLabel.text = @"待发货";
        }
            break;
        case 2:
        {
            self.typeLabel.text = @"待收货";
        }
            break;
        case 3:
        {
            self.typeLabel.text = @"待评价";
        }
            break;
        case 4:
        {
            self.typeLabel.text = @"已评价";
        }
            break;
    }
}

- (NSAttributedString *)getAttributeName:(NSString *)money{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *moneyAtt = [[NSAttributedString alloc]initWithString:@"累计    " attributes:@{NSForegroundColorAttributeName:getColor(textColor)}];
    [attStr appendAttributedString:moneyAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"¥"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:money]];
    
    return attStr;
}

- (NSAttributedString *)getAttributeName:(NSString *)name money:(NSString *)money number:(NSString *)number{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"单价  ¥"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:money]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" /数量 "]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:number]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

#pragma mark - click

- (void)chooseButtonClick:(UIButton *)sender{
    if (_getChooseClick) {
        _getChooseClick();
    }
}

@end
